package com.ftg.learn.mybatis.chapter2.service.impl;

import com.ftg.learn.mybatis.chapter2.dao.SaleDao;
import com.ftg.learn.mybatis.chapter2.entity.Sale;
import com.ftg.learn.mybatis.chapter2.entity.SaleFramer;
import com.ftg.learn.mybatis.chapter2.service.ISale;
import com.ftg.learn.mybatis.chapter2.util.ResultBean;
import com.ftg.learn.mybatis.chapter2.util.TipMess;
import com.ftg.learn.mybatis.chapter2.vo.SaleDetail;

import java.util.List;
import java.util.Objects;

/**
 * 实现类，完成接口真实方法
 *
 * @author kangjx
 * @date 2022/10/12
 */
public class SaleImpl implements ISale {

    SaleDao saleDao = new SaleDao();

    @Override
    public int save(Sale sale) {
        Sale one1 = saleDao.findOneById(sale.getId());
        if (Objects.nonNull(one1)) {
            throw new RuntimeException(TipMess.CURRENT_SALE_EXISTS);
        }

        SaleFramer one = saleDao.framerById(sale.getFid());
        if (Objects.isNull(one)) {
            throw new RuntimeException(TipMess.FRAMEER_NOT_EXIST);
        }
        return saleDao.saveSale(sale);
    }

    @Override
    public int modify(Sale sale) {
        Sale temp = saleDao.findOneById(sale.getId());
        if (Objects.isNull(temp)) {
            throw new RuntimeException(TipMess.SALE_NOT_EXIST);
        }
        return saleDao.modifySale(sale);
    }

    @Override
    public Sale findOneSale(Integer id) {
        return saleDao.findOneById(id);
    }

    @Override
    public int deleteLogic(Integer id) {
        Sale temp = saleDao.findOneById(id);
        if (Objects.nonNull(temp)) {
            return saleDao.deleteSale(id);
        }
        return 0;
    }

    @Override
    public List<SaleDetail> showDetailWithId(Integer id) {
        return saleDao.showDetailById(id);
    }

    @Override
    public ResultBean<SaleDetail> page(long page, long limit) {
        ResultBean<SaleDetail> resultBean = new ResultBean<>(page, limit);
        List<SaleDetail> details = saleDao.page(resultBean);
        // 就是我们的data当前页显示的数据
        resultBean.setData(details);
        resultBean.setTotal(saleDao.getConunt());
        return resultBean;
    }

}
